# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：batch_image.py
#日期：2019-05-31
#备注：通过PIL库处理图片（png、jpg、gif等）    pip install pillow  
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
from pycacho.cachobase.deal_image import create_background,crop_image,resize_image
from pycacho.cachobase.files_select import get_files
import os
from PIL import Image

#crop_box = (0, 0, 525, 72)  pixel =  (255, 255, 255)
# crop_box = (5, 670, 400, 715) pixel =  (178, 32, 45)
# (600, 668, 1280, 725), (52, 52, 52)

input_path = 'F:\\alibaba\产品图片\KARLER产品图片\KR-800\\'
output_path = 'F:\\alibaba\产品图片\KARLER产品图片\KR-800\\'
if not os.path.exists(output_path):
    os.makedirs(output_path)
files = get_files(input_path, 'jpg')

# 对统一规格的图片进行批量裁剪
def batch_crop_image():
    i = 0
    for file in files:
        print(file)
        img = Image.open(file)
        k = img.width  # k-宽度，
        g = img.height  # g-高度
        if k==1500:
            i = i + 1
            output_file = output_path + 'KR-800_b' + str(i) + '.jpg'
            if k<g:
                crop_box = (0, 10, k, k+10)  # （左，上，右，下）
                crop_image(file,output_file,crop_box)
            else:
                create_background(file,'H2',0,output_file)

def batch_resize_image():
    i = 0
    j = 0
    for file in files:
        print(file)
        img = Image.open(file)
        k = img.width  # k-宽度，
        g = img.height  # g-高度
        box = (1500, int(1500 / k * g / 2) * 2)
        if k>1500 and g>k:
            i = i + 1
            output_file = output_path + 'KR-800_b' + str(i) + '.jpg'
        else:
            j = j + 1
            output_file = output_path + 'KR-800_d' + str(j) + '.jpg'
        resize_image(file,output_file,box)

if __name__ == '__main__':
    batch_resize_image()
    batch_crop_image()